<template>
  <el-dialog class='subject-status-add-record' title="排序" width="600px" :show-close='true'
             :visible.sync="sortVisible" :close-on-click-modal="false" :before-close="handleClose">
    <div>
      <el-form :rules="rules" ref="formRef" size="mini" :model="formData" label-width="140px">
        <el-form-item label="目标顺序：" prop="targetId">
          <el-select v-model="formData.targetId" placeholder="请选择" style="width: 80%">
            <el-option v-for="item in sortList" :key="item.id" :label="item.variableName || item.embeddedSurfaceName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleClose">取 消</el-button>
      <el-button :loading='loading' size="small" type="primary" @click="handleSave">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { mapActions } from 'vuex'

export default {
  props: {
    sortData: Object,
    sortVisible: Boolean,
    sortList: Array
  },
  data() {
    return {
      rules: {
        targetId: [{ required: true, message: '请选择', trigger: 'blur' }],
      },
      loading: false,
      defaultFormData: {
      },
      formData: {}
    }
  },

  methods: {
    ...mapActions('eCRFDesign', ['sortInterfaceDesign']),

    // 关闭取消
    handleClose() {
      this.formData = { ...this.defaultFormData }
      this.$emit('handleSortCancel')
    },

    // 保存提交
    handleSave() {
      this.loading = true
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          const data = {
            optionId: this.sortData.id,
            targetId: this.formData.targetId
          }
          this.sortInterfaceDesign({ data }).then(res => {
            const { code } = res
            if (code === 200) {
              this.$message({
                message: '保存成功',
                type: 'success'
              })
              this.$refs.formRef.resetFields()
              this.$emit('handleSortCancel', 'refresh')
            }
            this.loading = false
          })
          
        } else {
          this.loading = false
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
@import './add-interfacial.scss';
</style>
